package com.grsu.phonestation.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.grsu.phonestation.domain.Admin;
import com.grsu.phonestation.domain.Client;
import com.grsu.phonestation.domain.Subscribe;
import com.grsu.phonestation.domain.User;
import com.grsu.phonestation.service.SubscribeService;
import com.grsu.phonestation.service.UserService;

@Controller
@RequestMapping("/subscribes")
public class SubscribesController {

	@Autowired
	private SubscribeService subscribeService;
	
	@Autowired
	@Qualifier("userServiceImpl")
	private UserService<User> userService;

	@RequestMapping(value = { "/", " " }, method = { RequestMethod.GET })
	public ModelAndView listSubscribesClient(HttpSession session, Model model) {

		ModelAndView mav = new ModelAndView();

		Object userObject = session.getAttribute("user");
		if (userObject != null) {
			if (userObject instanceof Client) {

				Client user = (Client) userObject;

				List<Subscribe> subscribes = subscribeService
						.getSubscribeServiceClient(user);

				mav.addObject("user", user);
				mav.addObject("subscribes", subscribes);
				mav.setViewName("subscribes");
			}
			if (userObject instanceof Admin) {
				Admin user = (Admin) userObject;

				List<Subscribe> subscribes = subscribeService.listSubscribees();

				mav.addObject("user", user);
				mav.addObject("subscribes", subscribes);
				mav.setViewName("admin.subscribes");
			}
		} else {
			mav = new ModelAndView("redirect:/login");
			mav.setViewName("login");
		}

		return mav;
	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public ModelAndView deliteSubscribeClient(
			@PathVariable("id") Integer subscribeId, HttpSession session,
			Model model) {

		ModelAndView mav = new ModelAndView("redirect:/subscribes/");

		subscribeService.removeSubscribe(subscribeId);
		
		return mav;
	}
	
	@RequestMapping(value = "/search", method = { RequestMethod.POST })
	public ModelAndView findSubscribeUser(
			@RequestParam(value = "user", required = true) String user,
			HttpServletRequest request, HttpSession session) {

		ModelAndView mav = new ModelAndView("redirect:/subscribes");

		List<Subscribe> subscribes = subscribeService.getSubscribeSearchUser(user);
		mav.addObject("subscribes",subscribes);
		mav.setViewName("admin.subscribes");
		

		return mav;
	}
	
}

